package com.gutai.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gutai.admin.common.entity.response.Response;
import com.gutai.admin.common.util.ExceptionUtils;
import com.gutai.admin.common.util.JsonUtil;
import com.gutai.admin.common.util.ResponseHelper;
import com.gutai.admin.entity.Coupon;
import com.gutai.admin.entity.UserCoupon;
import com.gutai.admin.service.ICouponService;

/**
 * 
 * 描述：优惠券
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年11月7日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    private static final Logger logger = LoggerFactory.getLogger(CouponController.class);

    @Autowired
    private ICouponService couponService;

    @CrossOrigin
    @GetMapping("/listAll")
    public Response listAll(String name, Integer startPage, Integer pageSize) {
        try {
            if (StringUtils.isNotBlank(name)) {
                List<Coupon> list = couponService.findByName(name);
                return ResponseHelper.buildOk(list);
            } else {
                if (startPage == null || startPage < 1) {
                    startPage = 1;
                }
                if (pageSize == null) {
                    pageSize = 10;
                }
                Page<Coupon> list = couponService.getList(startPage, pageSize);
                return ResponseHelper.buildOk(list.getContent(), list.getTotalElements());
            }
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/selectByUser")
    public Response selectList(Integer userId) {
        try {
            List<UserCoupon> datas = couponService.getByUserId(userId);
            return ResponseHelper.buildOk(datas);
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/queryById")
    public Response queryById(Integer id) {
        try {
            Coupon data = couponService.findById(id);
            if (data!=null){
                return ResponseHelper.buildOk(data);
            }
            return ResponseHelper.buildFail("Not found");
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @PostMapping("/add")
    public Response add(@RequestBody Coupon entity) {
        try {
            couponService.add(entity);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败" + e.getMessage());
        }
    }

    @CrossOrigin
    @PostMapping("/update")
    public Response update(@RequestBody Coupon entity) {
        try {
            couponService.update(entity);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败" + e.getMessage());
        }
    }

    @CrossOrigin
    @PostMapping("/editCoupon")
    public Response editCouponByUser(HttpServletRequest request, HttpServletResponse response) {
        try {
            String userId = request.getParameter("userId");
            String coupons = request.getParameter("coupons");
            List<UserCoupon> datas = JsonUtil.json2List(UserCoupon.class, coupons);
            couponService.updateAll(datas, Integer.valueOf(userId));
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/delete")
    public Response delete(Integer id) {
        try {
            couponService.delete(id);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败" + e.getMessage());
        }
    }

}
